package com.plm.k8s.myk8sapi.cmd.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.plm.k8s.myk8sapi.cmd.AbstractLocalCmd;
import com.plm.k8s.myk8sapi.cmd.INamespaceCmd;
import com.plm.k8s.myk8sapi.pojo.entity.ns.NamespaceEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author 潘磊明
 * 版本号：1.0
 * 创建日期：2021/7/16 15:22
 */
@Service
public class NamespaceCmdImpl extends AbstractLocalCmd implements INamespaceCmd {

    public NamespaceCmdImpl() {}

    public NamespaceCmdImpl(String kubectlPath, String kubeConfig) {
        this.kubectlPath = kubectlPath;
        this.kubeConfig = kubeConfig;
    }

    @Override
    public List<NamespaceEntity> listNamespace() {
        return listNamespace(null, null);
    }

    @Override
    public List<NamespaceEntity> listNamespace(String nameFilter, Map<String, String> labelFilter) {
        String cmd = generateListNamespace(nameFilter, labelFilter);
        List<String> list = execForLine(cmd);
        List<NamespaceEntity> result = parseListResult(list);
        return result;
    }

    @Override
    public boolean createNamespace(String namespace) throws Exception {
        if (!StringUtils.hasText(namespace)) throw new Exception("namespace名称不能为空");
        String cmd = generateCreateNamespace(namespace);
        String result = exec(cmd);
        if (!String.format("namespace/%s created", namespace).equals(trimBr(result))) {
            throw new Exception(result);
        }
        return true;
    }

    @Override
    public boolean deleteNamespace(String namespace) throws Exception {
        if (!StringUtils.hasText(namespace)) throw new Exception("namespace名称不能为空");
        String cmd = generateDeleteCmd(namespace);
        String result = exec(cmd);
        if (!String.format("namespace \"%s\" deleted", namespace).equals(trimBr(result))) {
            throw new Exception(result);
        }
        return true;
    }

    private String generateDeleteCmd(String namespace) {
        StringBuilder sb = new StringBuilder(generateKubeCmdBase());
        sb.append("delete ns ").append(namespace);
        return sb.toString();
    }

    private String generateCreateNamespace(String namespace) {
        StringBuilder sb = new StringBuilder(generateKubeCmdBase());
        sb.append("create ns ").append(namespace);
        return sb.toString();
    }

    private List<NamespaceEntity> parseListResult(List<String> list) {
        List<NamespaceEntity> result = new LinkedList<>();
        int startIndex = Integer.MAX_VALUE;
        for (int i = 0; i < list.size(); i++) {
            String tmp = list.get(i);
            if (tmp.startsWith("NAME")) {
                startIndex = i;
                continue;
            }
            if (i <= startIndex) continue;
            String[] array = tmp.split("\\s+");
            if (array.length < 3) continue;
            NamespaceEntity namespace = new NamespaceEntity();
            namespace.setName(array[0]);
            namespace.setStatus(array[1]);
            namespace.setAge(array[2]);
            result.add(namespace);
        }
        return result;
    }

    private String generateListNamespace(String nameFilter, Map<String, String> labelFilter) {
        StringBuilder sb = new StringBuilder(generateKubeCmdBase());
        sb.append("get ns");
        if (StringUtils.hasText(nameFilter)) {
            addNameFilter(sb, nameFilter);
        }
        if (!CollectionUtil.isEmpty(labelFilter)) addLabelFilter(sb, labelFilter);
        return sb.toString();
    }
}
